package com.jrx.anytxn.customer.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.constant.BankCardConstant;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.constant.ErrorMsgConstant;
import com.jrx.anytxn.customer.dto.BaseApiRes;
import com.jrx.anytxn.customer.dto.bankcardinfo.*;
import com.jrx.anytxn.customer.entity.CmCustomerBankcardInfo;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.customer.enums.CustomerMaintenanceType;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerBankcardInfoMapper;
import com.jrx.anytxn.customer.plugins.HistoryLog;
import com.jrx.anytxn.customer.plugins.HistoryLogHelper;
import com.jrx.anytxn.customer.service.IBankCardService;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * 银行卡绑定接口service
 *
 * @author 90443
 * @date 2019-07-18 16:16
 **/
@Service
public class BankCardServiceImpl implements IBankCardService {
    private Logger logger = LoggerFactory.getLogger(BankCardServiceImpl.class);
    @Resource
    private ExtCmCustomerBankcardInfoMapper extCmCustomerBankcardInfoMapper;

    @Autowired
    private ISegmentService segmentService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BankCardBindRes saveBankCard(BankCardBindReq bankCardBindReq, CmCustomerSecondLevel customersInfo, CmCustomerBankcardInfo customerBankcardInfo) throws TxnBizException {
        try {
            //初始化银行卡信息
            CmCustomerBankcardInfo cmCustomerBankcardInfo = new CmCustomerBankcardInfo();
            //配置银行卡相关信息
            BeanMapping.copy(bankCardBindReq, cmCustomerBankcardInfo);
            cmCustomerBankcardInfo.setContractStatus(CustomerConstant.STATUS_NORMAL);
            cmCustomerBankcardInfo.setUpdateBy(bankCardBindReq.getUserId());
            cmCustomerBankcardInfo.setRecordVersionNumber(Constant.VERSION_NUMBER);
            cmCustomerBankcardInfo.setCustomerId(customersInfo.getCustomerId());
            cmCustomerBankcardInfo.setOrganizationId(customersInfo.getOrganizationId());
            cmCustomerBankcardInfo.setCardStatus(BankCardConstant.STATUS_BANKCARD_1);
            cmCustomerBankcardInfo.setCardAddTime(new Date());

            HistoryLog txnHistoryLog = new HistoryLog(customersInfo.getCustomerId(), bankCardBindReq.getTenantId(), customersInfo.getOrganizationId(), bankCardBindReq.getChannel(), customersInfo.getUserId(), CustomerMaintenanceType.BANKCARD_SETFLAG.getCode());
            HistoryLogHelper.setLocalHistory(txnHistoryLog);

            //根据入参即银行卡绑定类型设置银行卡的默认收还款卡类型和默认收款卡时间  更新原默认卡标识
            setDefaultFlag(cmCustomerBankcardInfo, bankCardBindReq.getBindType());

            //此客户的此卡号的银行卡信息是否存在，customerBankcardInfo为空与否确定是增加还是修改
            if (customerBankcardInfo == null) {
                //不存在，取上面初始化的银行卡信息
                try {
                    cmCustomerBankcardInfo.setId(segmentService.getId(Constant.CM_USTOMER_BANKCARD_INFO));
                } catch (SegmentException e) {
                    logger.error("主键生成错误", e);
                    throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
                }
                extCmCustomerBankcardInfoMapper.insertSelective(cmCustomerBankcardInfo);
            } else {
                //存在,根据租户ID，客户ID，卡号修改银行卡信息，修改的银行卡信息为上初始化的银行卡信息
                cmCustomerBankcardInfo.setId(customerBankcardInfo.getId());
                logger.debug("存在 则改变其有效状态");
                //增添历史维护信息
                txnHistoryLog.setType(CustomerMaintenanceType.BANKCARD_UNBAND.getCode());
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(cmCustomerBankcardInfo);
            }

            //设置绑卡的响应实体
            BankCardBindRes bankCardBindRes = BeanMapping.copy(bankCardBindReq, BankCardBindRes.class);
            bankCardBindRes.setCardId(cmCustomerBankcardInfo.getId().toString());
            return bankCardBindRes;
        } finally {
            HistoryLogHelper.clearLocalHistory();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseApiRes modifyBankCardInfo(BankCardUnBindReq bankCardUnBindReq, String customerId, String userId) throws TxnBizException {
        try {
            //得到有效状态的该卡片信息，若该卡片不存在，校验不通过，抛异常
            CmCustomerBankcardInfo bankCardUnBindInfo = getCmCustomerBankcardInfo(customerId, bankCardUnBindReq.getChannel(), bankCardUnBindReq.getCardNumber(), BankCardConstant.STATUS_BANKCARD_1, bankCardUnBindReq.getTenantId());

            //判断除该卡片外，客户是否有其他已绑定的有效状态的卡片（客户id+渠道+卡号，状态为绑定）
            //得到除了该卡片之外的更新时间最早的银行卡信息
            List<CmCustomerBankcardInfo> customerBankcardInfoList = extCmCustomerBankcardInfoMapper.selectByCustomerIdAndChannelAndCardNumber(customerId, bankCardUnBindReq.getChannel(), bankCardUnBindReq.getCardNumber(), BankCardConstant.STATUS_BANKCARD_1, bankCardUnBindReq.getTenantId());
            if (CollectionUtils.isEmpty(customerBankcardInfoList)) {
                bankCardUnBindInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
                bankCardUnBindInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
                //对该卡片做解除绑定处理,设置相应的修改参数信息，执行修改操作
                bankCardUnBindInfo.setCardStatus(BankCardConstant.STATUS_BANKCARD_0);
                bankCardUnBindInfo.setUpdateBy(bankCardUnBindReq.getUserId());

                HistoryLog txnHistoryLog = new HistoryLog(bankCardUnBindInfo.getCustomerId(), bankCardUnBindReq.getTenantId(), bankCardUnBindInfo.getOrganizationId(), bankCardUnBindInfo.getChannel(), userId, CustomerMaintenanceType.BANKCARD_UNBAND.getCode());
                HistoryLogHelper.setLocalHistory(txnHistoryLog);
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(bankCardUnBindInfo);
                return BeanMapping.copy(bankCardUnBindReq, BaseApiRes.class);
            }

            //获取客户其他有效状态卡片中绑卡时间最早的那张卡片
            CmCustomerBankcardInfo bankcardInfoEarly = customerBankcardInfoList.get(0);

            //根据之前卡的默认还/收款来设置新值
            if (bankCardUnBindInfo.getDefaultGetFlag().equalsIgnoreCase(BankCardConstant.DEFAULT_FLAG_Y)) {
                bankcardInfoEarly.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_Y);
                bankcardInfoEarly.setCardDefaultGetTime(new Date());
                bankCardUnBindInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
            }
            if (bankCardUnBindInfo.getDefaultPayFlag().equalsIgnoreCase(BankCardConstant.DEFAULT_FLAG_Y)) {
                bankcardInfoEarly.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_Y);
                bankcardInfoEarly.setCardDefaultPayTime(new Date());
                bankCardUnBindInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
            }

            //对该卡片做解除绑定处理,设置相应的修改参数信息，执行修改操作
            bankCardUnBindInfo.setCardStatus(BankCardConstant.STATUS_BANKCARD_0);
            bankCardUnBindInfo.setUpdateBy(bankCardUnBindReq.getUserId());

            HistoryLog txnHistoryLog = new HistoryLog(bankCardUnBindInfo.getCustomerId(), bankCardUnBindReq.getTenantId(), bankCardUnBindInfo.getOrganizationId(), bankCardUnBindInfo.getChannel(), userId, CustomerMaintenanceType.BANKCARD_UNBAND.getCode());
            HistoryLogHelper.setLocalHistory(txnHistoryLog);
            extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(bankCardUnBindInfo);

            //将客户其他有效状态卡片中绑卡时间最早的那张卡片,设置为放款卡/还款卡处理
            bankcardInfoEarly.setUpdateBy(bankCardUnBindReq.getUserId());
            bankcardInfoEarly.setRecordVersionNumber(bankcardInfoEarly.getRecordVersionNumber() + 1);
            //修改历史维护信息类型
            txnHistoryLog.setType(CustomerMaintenanceType.BANKCARD_SETFLAG.getCode());
            //对最早更新的有效卡信息进行更新
            extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(bankcardInfoEarly);

            return BeanMapping.copy(bankCardUnBindReq, BaseApiRes.class);
        } finally {
            HistoryLogHelper.clearLocalHistory();
        }
    }

    @Override
    public List<BankCardRes> getBankCardList(String customerId, String channel, String status, String tenantId) {
        //根据客户id+渠道查询客户银行卡信息表，获得客户当前所有绑定状态的银行卡列表
        logger.debug("1.获得客户当前所有绑定状态的银行卡列表,并将值返回到map集合里");
        List<CmCustomerBankcardInfo> customerBankcardInfoList = extCmCustomerBankcardInfoMapper.selectByChannelCusId(customerId, channel, status, tenantId);
        if (CollectionUtils.isEmpty(customerBankcardInfoList)) {
            return Collections.emptyList();
        } else {
            List<BankCardRes> bankCardResList = new ArrayList<>(customerBankcardInfoList.size());
            Consumer<CmCustomerBankcardInfo> bankcardInfoConsumer = customerBankcardInfo -> {
                BankCardRes bankCardRes = new BankCardRes();
                bankCardRes.setCardId(customerBankcardInfo.getId().toString());
                bankCardRes.setCardNumber(customerBankcardInfo.getCardNumber());
                bankCardRes.setBankName(customerBankcardInfo.getBankName());
                bankCardRes.setCardNoLast(customerBankcardInfo.getCardNumber().substring(customerBankcardInfo.getCardNumber().length() - 4));
                bankCardRes.setBankId(customerBankcardInfo.getBankId());
                bankCardRes.setCardType(customerBankcardInfo.getCardType());
                bankCardRes.setDefaultLoanCard(customerBankcardInfo.getDefaultGetFlag());
                bankCardRes.setDefaultPaymentCard(customerBankcardInfo.getDefaultPayFlag());
                bankCardRes.setMobile(customerBankcardInfo.getMobile());
                bankCardRes.setContractId(customerBankcardInfo.getContractId());
                bankCardRes.setCreateTime(customerBankcardInfo.getCreateTime());
                bankCardResList.add(bankCardRes);
            };
            customerBankcardInfoList.forEach(bankcardInfoConsumer);
            return bankCardResList;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseApiRes modifyBankCardSetFlag(BankCardSetFlagReq bankCardSetFlagReq, String customerId) throws TxnBizException {
        try {
            //得到有效状态的该卡片信息，若该卡片不存在，校验不通过，抛异常
            CmCustomerBankcardInfo customerBankcardInfo = getCmCustomerBankcardInfo(customerId, bankCardSetFlagReq.getChannel(), bankCardSetFlagReq.getCardNumber(), BankCardConstant.STATUS_BANKCARD_1, bankCardSetFlagReq.getTenantId());
            //修改该卡片相关属性信息
            customerBankcardInfo.setUpdateBy(bankCardSetFlagReq.getUserId());

            HistoryLog txnHistoryLog = new HistoryLog(customerId, bankCardSetFlagReq.getTenantId(), customerBankcardInfo.getOrganizationId(), bankCardSetFlagReq.getChannel(), bankCardSetFlagReq.getUserId(), CustomerMaintenanceType.BANKCARD_SETFLAG.getCode());
            HistoryLogHelper.setLocalHistory(txnHistoryLog);

            setDefaultFlag(customerBankcardInfo, bankCardSetFlagReq.getFlagType());

            extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(customerBankcardInfo);


            return BeanMapping.copy(bankCardSetFlagReq, BaseApiRes.class);
        } finally {
            HistoryLogHelper.clearLocalHistory();
        }
    }

    @Override
    public CmCustomerBankcardInfo getBankByChannleAndCusIdAndCardNum(String customerId, String channel, String cardNum, String tenantId) {
        CmCustomerBankcardInfo cmCustomerBankcardInfos = extCmCustomerBankcardInfoMapper.selectByBankcard(customerId, channel, cardNum, null, tenantId);
        return cmCustomerBankcardInfos;
    }

    @Override
    public CmCustomerBankcardInfo getBankCardByCondition(String customerId, String defaultPayFlag, String cardNumber, String channel, String tenantId) throws TxnBizException {
        CmCustomerBankcardInfo cmCustomerBankcardInfo = extCmCustomerBankcardInfoMapper.selectBankCardByCondition(customerId, defaultPayFlag, cardNumber, channel, tenantId);
        if (cmCustomerBankcardInfo == null) {
            logger.error("客户银行卡信息为空");
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getUserTips());
        }
        return cmCustomerBankcardInfo;
    }

    @Override
    public ArrayList<BankCardInfoRes> getBankCardList(String channel, String customerId, String tenantId) {
        CmCustomerBankcardInfo cond = new CmCustomerBankcardInfo();
        cond.setCardStatus(BankCardConstant.STATUS_BANKCARD_1);
        cond.setCustomerId(customerId);
        cond.setChannel(channel);
        cond.setTenantId(tenantId);
        List<CmCustomerBankcardInfo> ls = extCmCustomerBankcardInfoMapper.selectByCondition(cond);

        List<BankCardInfoRes> res = BeanMapping.copyList(ls, BankCardInfoRes.class);
        return (ArrayList<BankCardInfoRes>) res;
    }

    /**
     * 卡片信息是否已存在校验，若不存在该渠道有效状态的卡片信息（客户id+渠道+卡号，状态为绑定）则校验不通过
     *
     * @param customerId 客户ID
     * @param channel    渠道号
     * @param cardNumber 卡号
     * @param status     状态
     * @param tenantId   租户ID
     * @return com.jrx.anytxn.customer.entity.CmCustomerBankcardInfo
     * @author Yangxin
     * date: 2019/10/21 0021 下午 13:42
     */
    private CmCustomerBankcardInfo getCmCustomerBankcardInfo(String customerId, String channel, String cardNumber, String status, String tenantId) throws TxnBizException {
        CmCustomerBankcardInfo customerBankcardInfo = extCmCustomerBankcardInfoMapper.selectByBankcard(customerId, channel, cardNumber, status, tenantId);
        //卡片信息是否已存在校验，若不存在该渠道有效状态的卡片信息（客户id+渠道+卡号，状态为绑定）则校验不通过；
        if (customerBankcardInfo == null) {
            logger.error(ErrorMsgConstant.BANKCARD_NODATA);
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getUserTips());
        }
        return customerBankcardInfo;
    }

    /**
     * 根据当前卡片默认放/还卡信息设置其他有效卡片状态
     * 保证客户在统一渠道下只有一张默认放/还卡， 二者可以是同一张卡
     *
     * @param cmCustomerBankcardInfo 客户银行卡信息
     * @param bindType               绑定类型 0：放/还卡 1：放
     * @return 卡片状态信息
     */
    private void setDefaultFlag(CmCustomerBankcardInfo cmCustomerBankcardInfo, String bindType) {
        //根据入参设置默认收还款卡类型，设置其他卡默认收还款卡类型
        if (bindType.equalsIgnoreCase(BankCardConstant.BINDTYPE_0)) {
            cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_Y);
            cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_Y);
            cmCustomerBankcardInfo.setCardDefaultPayTime(new Date());
            cmCustomerBankcardInfo.setCardDefaultGetTime(new Date());
            CmCustomerBankcardInfo defaultGetCard = null;
            CmCustomerBankcardInfo defaultPayCard = null;
            List<CmCustomerBankcardInfo> list = extCmCustomerBankcardInfoMapper.selectByDefaultGetFlag(cmCustomerBankcardInfo.getCustomerId(), cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId(), BankCardConstant.DEFAULT_FLAG_Y);
            if (CollectionUtils.isNotEmpty(list)) {
                defaultGetCard = list.get(0);
            }
            defaultPayCard = extCmCustomerBankcardInfoMapper.selectBankCardByCondition(cmCustomerBankcardInfo.getCustomerId(), BankCardConstant.DEFAULT_FLAG_Y, null, cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId());
            if (defaultGetCard != null && !defaultGetCard.getCardNumber().equals(cmCustomerBankcardInfo.getCardNumber())) {
                CmCustomerBankcardInfo updateGetCard = new CmCustomerBankcardInfo();
                updateGetCard.setId(defaultGetCard.getId());
                updateGetCard.setTenantId(defaultGetCard.getTenantId());
                updateGetCard.setCustomerId(defaultGetCard.getCustomerId());
                updateGetCard.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
                updateGetCard.setUpdateBy(cmCustomerBankcardInfo.getUpdateBy());
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(updateGetCard);
            }
            if (defaultPayCard != null && !defaultPayCard.getCardNumber().equals(cmCustomerBankcardInfo.getCardNumber())) {
                CmCustomerBankcardInfo updatePayCard = new CmCustomerBankcardInfo();
                updatePayCard.setId(defaultPayCard.getId());
                updatePayCard.setTenantId(defaultPayCard.getTenantId());
                updatePayCard.setCustomerId(defaultPayCard.getCustomerId());
                updatePayCard.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
                updatePayCard.setUpdateBy(cmCustomerBankcardInfo.getUpdateBy());
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(updatePayCard);
            }
        } else if (bindType.equalsIgnoreCase(BankCardConstant.BINDTYPE_1)) {
            cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_Y);
//            cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
            cmCustomerBankcardInfo.setCardDefaultGetTime(new Date());
            CmCustomerBankcardInfo defaultGetCard = null;
            CmCustomerBankcardInfo defaultPayCard = null;
            defaultPayCard = extCmCustomerBankcardInfoMapper.selectBankCardByCondition(cmCustomerBankcardInfo.getCustomerId(), BankCardConstant.DEFAULT_FLAG_Y, null, cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId());
            if(defaultPayCard==null){
                cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_Y);
                cmCustomerBankcardInfo.setCardDefaultPayTime(new Date());
            }else{
                if(StringUtils.isBlank(cmCustomerBankcardInfo.getDefaultPayFlag())){
                    cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
                }
            }
            List<CmCustomerBankcardInfo> list = extCmCustomerBankcardInfoMapper.selectByDefaultGetFlag(cmCustomerBankcardInfo.getCustomerId(), cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId(), BankCardConstant.DEFAULT_FLAG_Y);
            if (CollectionUtils.isNotEmpty(list)) {
                defaultGetCard = list.get(0);
            }
            if (defaultGetCard != null && !defaultGetCard.getCardNumber().equals(cmCustomerBankcardInfo.getCardNumber())) {
                CmCustomerBankcardInfo updateGetCard = new CmCustomerBankcardInfo();
                updateGetCard.setId(defaultGetCard.getId());
                updateGetCard.setTenantId(defaultGetCard.getTenantId());
                updateGetCard.setCustomerId(defaultGetCard.getCustomerId());
                updateGetCard.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
                updateGetCard.setUpdateBy(cmCustomerBankcardInfo.getUpdateBy());
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(updateGetCard);
            }
        } else if (bindType.equalsIgnoreCase(BankCardConstant.BINDTYPE_2)) {
            cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_Y);
//            cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
            cmCustomerBankcardInfo.setCardDefaultPayTime(new Date());
            CmCustomerBankcardInfo defaultPayCard = null;
            List<CmCustomerBankcardInfo> list = extCmCustomerBankcardInfoMapper.selectByDefaultGetFlag(cmCustomerBankcardInfo.getCustomerId(), cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId(), BankCardConstant.DEFAULT_FLAG_Y);
            if (CollectionUtils.isEmpty(list)) {
                cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_Y);
                cmCustomerBankcardInfo.setCardDefaultGetTime(new Date());
            }else{
                if(StringUtils.isBlank(cmCustomerBankcardInfo.getDefaultGetFlag())){
                    cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
                }
            }
            defaultPayCard = extCmCustomerBankcardInfoMapper.selectBankCardByCondition(cmCustomerBankcardInfo.getCustomerId(), BankCardConstant.DEFAULT_FLAG_Y, null, cmCustomerBankcardInfo.getChannel(), cmCustomerBankcardInfo.getTenantId());
            if (defaultPayCard != null && !defaultPayCard.getCardNumber().equals(cmCustomerBankcardInfo.getCardNumber())) {
                CmCustomerBankcardInfo updatePayCard = new CmCustomerBankcardInfo();
                updatePayCard.setId(defaultPayCard.getId());
                updatePayCard.setTenantId(defaultPayCard.getTenantId());
                updatePayCard.setCustomerId(defaultPayCard.getCustomerId());
                updatePayCard.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
                updatePayCard.setUpdateBy(cmCustomerBankcardInfo.getUpdateBy());
                extCmCustomerBankcardInfoMapper.updateByIdAndCustomerIdAndTenantId(updatePayCard);
            }
        } else {
            cmCustomerBankcardInfo.setDefaultPayFlag(BankCardConstant.DEFAULT_FLAG_N);
            cmCustomerBankcardInfo.setDefaultGetFlag(BankCardConstant.DEFAULT_FLAG_N);
        }
    }

    @Override
    public CmCustomerBankcardInfo getBankCardByCustomerId(String customerId, String defaultPayFlag, String channel, String tenantId){
        CmCustomerBankcardInfo cmCustomerBankcardInfo = extCmCustomerBankcardInfoMapper.selectBankCardByCondition(customerId, defaultPayFlag, null, channel, tenantId);
        return cmCustomerBankcardInfo;
    }
}
